(* COULEUR *)

Graphics.clear_graph () ;;

# load "graphics.cma" ;;

type couleur = Noir| Blanc| Rouge| Vert| Bleu| Random ;;

let my_set_color a b c = Graphics.set_color (Graphics.rgb a b c) ;;

Graphics.open_graph "784x806" ;;

let test () =
  let _ = my_set_color 242 142 242 in
      Graphics.fill_circle 142 142 42 ;;
test () ;;

(* PLUS DE COULEUR *)

Random.self_init() ;;

let color = function
  | Noir -> my_set_color  0 0 0
  | Blanc -> my_set_color 255 255 255
  | Rouge -> my_set_color 255 0 0
  | Vert -> my_set_color 0 255 0
  | Bleu -> my_set_color 0 0 255
  | Random -> my_set_color (Random.int(255)) (Random.int(255)) (Random.int(255)) ;;

let test c =
  let _ = color c in
    Graphics.fill_circle 142 142 42 ;;

test Rouge ;;

let draw_rect c x y w h =
  let _ = color c in
    Graphics.fill_rect x y w h ;;
draw_rect Bleu 50 50 500 500 ;;

(* UNE IMAGE *)

type image =
  | Uni of couleur
  | Comp of image*image*image*image ;;

let rec display img x y w h = match img with
  | Uni (c) -> draw_rect c x y w h
  | Comp (i1, i2, i3, i4) -> display i1 x y (w/2) (h/2);(*bas gauche*)
                             display i2 (x+w/2) y (w/2) (h/2);(*bas droite*)
                             display i3 x (y+h/2)  (w/2) (h/2);(*haut gauche*)
                             display i4 (x+w/2) (y+h/2) (w/2) (h/2);;(*haut droite*)

display (Comp ( Uni (Bleu), Uni (Noir), Uni (Rouge), Uni (Vert))) 50 50 500 500 ;;
display (Comp (image_simple, image_simple, image_simple, image_simple)) 50 50 500 500 ;;

let magic img = display img 0 0 (Graphics.size_x()) (Graphics.size_y()) ;;
magic (Comp ( Uni (Bleu), Uni (Noir), Uni (Rouge), Uni (Vert))) ;;

let image_simple = Comp (
  Uni (Noir),
  Uni (Rouge),
  Uni (Vert),
  Uni (Bleu)
) ;;

display (Comp (image_simple, image_simple, image_simple, image_simple)) 50 50 500 500 ;;

let rec frac4 img = Comp (img, img, img, img) ;;

let rec apply f img n = match n with
  | 1 -> f img
  | n -> 
    if n>0 then
     apply f (f img) (n-1)
    else
     invalid_arg "n droit etre strictement positif" ;;

magic (apply (frac4) (Uni Random) 9) ;;                   
